package com.youxue.teachin.splash;

import android.util.Log;
import com.library.rest.ApiServiceManager;
import com.library.rest.rxjava.RxSchedulersHelper;
import com.library.rest.vo.ResponseModel;
import com.library.uiframework.mvp.BasePresenter;
import com.youxue.teachin.common.utils.DefaultSubscriber;
import com.youxue.teachin.common.utils.PreContent;
import com.youxue.teachin.common.utils.PreferencesUtils;
import com.youxue.teachin.common.vo.UserVO;
import com.youxue.teachin.set.usercase.SetUserCase;
import com.youxue.teachin.user.userSystem.UserSystem;
import rx.Observable;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-08 15:37
 * @version: 9.1.0
 */
public class SplashPresenter extends BasePresenter<ISplashView> {
    /**
     * true 直接进入主页
     */
    boolean isIntoMain = false;

    /**
     * 构造方法
     *
     * @param iSplashView 绑定的视图对象
     */
    public SplashPresenter(ISplashView iSplashView) {
        super(iSplashView);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Observable<Boolean> toLancher = null;
        Log.e("致伟", this.getClass().getSimpleName() + " onCreate: =" + UserSystem.getInstance().isLogin());
        if (UserSystem.getInstance().isLogin()) {
            Observable<Boolean> userinfo = getUserInfo();
            Observable<Boolean> tabbar = getTabar();
            toLancher = userinfo.zipWith(tabbar, new Func2<Boolean, Boolean, Boolean>() {
                @Override
                public Boolean call(Boolean aBoolean, Boolean aBoolean2) {
                    return aBoolean && aBoolean;
                }
            });
        } else {
            toLancher = getTabar()
            .map(new Func1<Boolean, Boolean>() {
                @Override
                public Boolean call(Boolean aBoolean) {
                    //直接登录
                    return false;
                }
            });
        }

        toLancher
            .compose(RxSchedulersHelper.<Boolean>io_main())
            .subscribe(new DefaultSubscriber<Boolean>(){
                @Override
                public void onNext(Boolean aBoolean) {
                    super.onNext(aBoolean);
                    toNextPage(aBoolean);
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    toNextPage(false);
                }
            });
    }

    /**
     * 获取用户信息
     * @return
     */
    private Observable<Boolean> getUserInfo() {
        return new SetUserCase()
            .getUserInfo()
            .map(new Func1<UserVO, Boolean>() {
                @Override
                public Boolean call(UserVO userVO) {
                    return userVO != null;
                }
            }).subscribeOn(Schedulers.io());
    }

    /**
     * 获取tabbar h5地址
     * @return
     */
    private Observable<Boolean> getTabar() {
        Class<ItabBarService> claxx = ItabBarService.class;
        return ApiServiceManager.getOrCreateApiService(claxx)
            .getTabBarUrl()
            .map(new Func1<ResponseModel<TabBarVO>, Boolean>() {
                @Override
                public Boolean call(ResponseModel<TabBarVO> tab) {
                    if (tab != null && tab.data != null) {
                        TabBarVO tabBarVO = tab.data;
                        PreferencesUtils.saveString(PreContent.FAXIAN, tabBarVO.article);
                        PreferencesUtils.saveString(PreContent.YOUKE, tabBarVO.goodCourse);
                        PreferencesUtils.saveString(PreContent.ZHAILU, tabBarVO.collect);
                        PreferencesUtils.saveString(PreContent.JIAOXUE, tabBarVO.bookInfo);
                        return true;
                    }
                    return false;
                }
            })
            .subscribeOn(Schedulers.io());
    }

    private void toNextPage(boolean toLancher) {
        if (toLancher) {
            mBindView.toLauncher();
        } else {
            mBindView.toLogin();
        }
    }

}
